﻿/*
	[ru]	Пример загрузки текстур в отдельном потоке.
*/

#include "Engine.h"
using namespace Engine;



class CExample : public ICoreEventListener
{
private:
	CTextureMaterial	cTex1,
						cTex2,
						cTex3,
						cTex4;
	float				fLastTime;

public:
	CExample(): fLastTime(0)
	{
	}

	~CExample()
	{
	}
	
	void Draw();
	void Process();

	virtual void OnInit();
	virtual void OnFree();

	virtual void OnUpdate();

	virtual void Release()									{ delete this; }
	virtual e_win_event::type GetEvents()					{ return e_win_event::UNKNOWN; }

	virtual void ProcessMessage(const TWindowEvent &sEvent)	{}
	virtual void OnException()								{}
	
	static int s_RunEngine();
};




inline void CExample::Draw()
{
	RENDERGL()->ClearBuffers();

	BATCH()->Quad2dG( &cTex1, TQuad2D( rectf(-1.f,  0.f, 0.f, 1.f), rectf(0.f, 0.f, 1.f, 1.f) ) );
	BATCH()->Quad2dG( &cTex2, TQuad2D( rectf( 0.f,  0.f, 1.f, 1.f), rectf(0.f, 0.f, 1.f, 1.f) ) );
	BATCH()->Quad2dG( &cTex3, TQuad2D( rectf(-1.f, -1.f, 0.f, 0.f), rectf(0.f, 0.f, 1.f, 1.f) ) );
	BATCH()->Quad2dG( &cTex4, TQuad2D( rectf( 0.f, -1.f, 1.f, 0.f), rectf(0.f, 0.f, 1.f, 1.f) ) );

	BATCH()->Flush();
	BATCH()->Clear();

	RENDERGL()->SwapBuffers();
}


inline void CExample::Process()
{
	INPUT()->Update();

	if ( INPUT()->IsKey(e_key::ESCAPE) )
		CORE()->QuitEngine();

	INPUT()->Reset();
}


void CExample::OnInit()
{
	bool					ret		= true;
	e_tex_flags::type		e_flags	= e_tex_flags::BILINEAR | e_tex_flags::CLAMP_TO_EDGE | e_tex_flags::MIPMAPS;
	CBaseProgramShellPtr	p_prog;
	CTexture2DPtr			p_tex;
	e_def_program::type		e_prog_type = e_def_program::SIMPLE_2D_GS;

	ret &= RENDER()->GetDefaultProgram( e_prog_type, p_prog );

	ret &= RESMAN()->StreamLoad( e_res_group::TEXTURE, p_tex, "image1.tex",	e_flags );
	ret &= cTex1.Create( p_tex, p_prog );

	ret &= RESMAN()->StreamLoad( e_res_group::TEXTURE, p_tex, "image2.tex",	e_flags );
	ret &= cTex2.Create( p_tex, p_prog );

	ret &= RESMAN()->StreamLoad( e_res_group::TEXTURE, p_tex, "image3.tex",	e_flags );
	ret &= cTex3.Create( p_tex, p_prog );

	ret &= RESMAN()->StreamLoad( e_res_group::TEXTURE, p_tex, "image4.tex",	e_flags );
	ret &= cTex4.Create( p_tex, p_prog );

	MSTACK()->SetMatrix( mat4::BuildOrtho( rectf(-1.f, -1.f, 1.f, 1.f), vec2(-10.f, 10.f) ), e_transform::PROJECTION );

	RENDERGL()->SetClearColor( color4f(0.4f) );
	RENDERGL()->SetClearDepth( 1.f );

	RENDER()->AlphaTest( e_prog_type, false );
	RENDER()->CombineMode( e_prog_type, e_combine_mode::REPLACE );
	RENDER()->DiffuseColor( e_prog_type, color4f(1.f) );

	ASSERT( ret );
}


void CExample::OnFree()
{
}


void CExample::OnUpdate()
{
	CORE()->Update();
	Draw();
	
	fLastTime += CORE()->GetUpdateTimeDelta();

	if ( fLastTime > 1.f/60.f )
	{
		fLastTime = 0.f;
		Process();
	}
}


int CExample::s_RunEngine()
{
	CExample	*		p_example			= new CExample();
	TEngInitParams		s_init_params;
	e_log_output::bits	e_log_bits			= e_log_output::LOG_FILE_BIT
											DEBUG_ONLY( | e_log_output::MESSAGE_BIT )
											;
	
	s_init_params.eFlags					= e_eng_init_flags::OPENGL_4_2
											| e_eng_init_flags::CORE_PROFILE
											| e_eng_init_flags::EXTENDED_CONTEXT
											//| e_eng_init_flags::DEBUG_MODE
											| e_eng_init_flags::STREAMED_LOAD
											//| e_eng_init_flags::STEREO
											//| e_eng_init_flags::ES2_PROFILE
											;
	s_init_params.sApplicationName			= "Texture Streaming Sample";
	s_init_params.sClassName				= "";
	s_init_params.pHandle					= nullptr;
	s_init_params.pIcon						= nullptr;

	s_init_params.sAudioParams.sAudioDevice	= "";

	s_init_params.sRenInit.uColorDepth		= 32;
	s_init_params.sRenInit.uZBufferDepth	= 24;
	s_init_params.sRenInit.uStencilDepth	= 0;
	s_init_params.sRenInit.uColorSamples	= 0;
	s_init_params.sRenInit.uCSAASamples		= 0;
	s_init_params.sRenInit.uMSAASamples		= 0;
	s_init_params.sRenInit.uPixelFormat		= 0;

	s_init_params.sRenParams.bFullScreen	= false;
	s_init_params.sRenParams.bVSync			= true;
	s_init_params.sRenParams.eMode			= e_window_mode::ALLOW_SIZING | e_window_mode::CENTERED;
	s_init_params.sRenParams.sWinPos		= ivec2( 0, 0 );
	s_init_params.sRenParams.sWinSize		= ivec2( 800, 600 );
	
	s_init_params.sExtParams.uLogLevel		= 15;
	s_init_params.sExtParams.eLogFilter		= e_log_msg::FILTER_ALL;
	s_init_params.sExtParams.eResManFlags	= e_resman_flags::FREE_NON_REF | e_resman_flags::SET_DEFAUL_RES;
	s_init_params.sExtParams.sResDirectory	= "data";
	s_init_params.uLoadThreads				= 2;
	s_init_params.uRenderThreads			= 0;
	
	if ( !RunEngine( p_example, s_init_params, e_log_bits, "log.html", nullptr ) )
		return 1;

	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	return CExample::s_RunEngine();
}